package cn.stylefeng.guns.modular.rest.tool;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.app.model.params.MovingPalletsmergeParam;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.entity.BasicPosition;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicWarehouseParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicWarehouseResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.storage.entity.StorageStock;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.tool.entity.ToolStorageStock;
import cn.stylefeng.guns.modular.tool.model.params.ToolBtnShiftParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolRepairGoodsParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolStorageStockParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolStorageStockResult;
import cn.stylefeng.guns.modular.tool.service.ToolRepairGoodsService;
import cn.stylefeng.guns.modular.tool.service.ToolStorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 云仓库存
 */
@Controller
@RequestMapping("/rest/toolStorageStock")
public class restToolStorageStockController extends BaseController{

    @Autowired
    private ToolStorageStockService toolStorageStockService;
    @Autowired
    private UserAop userAop;

    @Autowired
    private BasicPositionService basicPositionService;

    /**
     * 查询库存总量
     */
    @RequestMapping("/findTotalBySpec")
    @ResponseBody
    public ResponseData findTotalBySpec(ToolStorageStockParam toolStorageStockParam,String pNumber) {
    	User user = userAop.getUser();
    	toolStorageStockParam.setStockstate("在库");
		if(ToolUtil.isNotEmpty(pNumber)) {
			BasicPositionParam basicPositionParam = new BasicPositionParam();
			basicPositionParam.setPositionNumber(pNumber);
			basicPositionParam.setDeliverId(user.getDeliverId());
			BasicPositionResult basicPositionResult = basicPositionService.findBySpec(basicPositionParam, user.getDeptId(),user.getOperateId());
			if(basicPositionResult == null) {
				return ResponseData.error(pNumber+"库位不存在！");
			} else {
				toolStorageStockParam.setPositionId(Long.parseLong(basicPositionResult.getId().toString()));
			}
		} 
    	ToolStorageStockResult ssr = toolStorageStockService.findTotalBySpec(toolStorageStockParam);
        return ResponseData.success(ssr);
    }
    
    /**
     * 查询商品总量，体积总量
     */
    @RequestMapping("/findGoodsListBySpec")
    @ResponseBody
    public ResponseData findGoodsListBySpec(ToolStorageStockParam toolStorageStockParam,String pNumber) {
    	User user = userAop.getUser();
    	toolStorageStockParam.setStockstate("在库");
		if(ToolUtil.isNotEmpty(pNumber)) {
			BasicPositionParam basicPositionParam = new BasicPositionParam();
			basicPositionParam.setPositionNumber(pNumber);
			basicPositionParam.setDeliverId(user.getDeliverId());
			BasicPositionResult basicPositionResult = basicPositionService.findBySpec(basicPositionParam, user.getDeptId(),user.getOperateId());
			if(basicPositionResult == null) {
				return ResponseData.error(pNumber+"库位不存在！");
			} else {
				toolStorageStockParam.setPositionId(Long.parseLong(basicPositionResult.getId().toString()));
			}
		} 
    	return ResponseData.success(toolStorageStockService.findGoodsListBySpec(toolStorageStockParam));
    }


}
